<?php

/**
 * A simple concrete implimentation of Log_XML_DOM.
 * 
 * PHP version 5.
 * 
 * @category Logging
 * @package  Log
 * @author   Michael Peter 'Vladimyr' Gower <designatevoid@gmail.com>
 * @author   "ilgarm" <ilgarm@azeronline.com>
 * @author   Jon Parise <jon@php.net>
 * @author   Roman Neuhauser <neuhauser@bellavista.cz>
 * @license  http://www.apache.org/licenses/LICENSE-2.0  Apache License 2.0
 * @link     http://pear.php.net/pepr/pepr-proposal-show.php?id=487
 */
/**
 * Require the base class.
 */ 
require_once 'Log/XML/DOM.php';

/**
 * The simplest implimentation of Log_XML you can do.
 *
 * @category Logging
 * @package  Log
 * @author   Michael Peter 'Vladimyr' Gower <designatevoid@gmail.com>
 * @license  http://www.apache.org/licenses/LICENSE-2.0  Apache License 2.0
 * @link     http://pear.php.net/pepr/pepr-proposal-show.php?id=487
 */
class Log_XML_DOM_Simple extends Log_XML_DOM
{
    /**
     * Add the header to the xml file.
     * 
     * @return boolean true on success, false on failure
     */
    protected function createHeader()
    {
        // add root SimpleLog node
        $node_root = $this->appendElement($this->domDocument, 'SimpleLog');
        
        // flush all
        $this->flush();
        
        return true;
    }
    
    
    /**
     * Creates the log entry in the xml file.
     * 
     * @param mixed $message the message to log
     * 
     * @return boolean true on success, false on failure
     */
    protected function createLogEntry($message)
    {        
        // get the root node
        $node_root = $this->domDocument
            ->getElementsByTagName('SimpleLog')->item(0);
        
        // add SimpleLog->Message
        $this->appendElement($node_root, 'Message', $message);
        
        // flush
        $this->flush();
        
        return true;
    }
    
    /**
     * This will append a new node with the name $string_name to the parent.
     * If $value is specified then we create text node attached to $string_name
     * as well.
     * 
     * @param DOMNode $node_parent The parent of the new element
     * @param string  $string_name The name of the new element
     * @param string  $value       The optional text value of the new element
     * 
     * @return DOMNode
     */
    protected function appendElement($node_parent, $string_name, $value = null)
    {
        $node_child = $this->domDocument->createElement($string_name);
        $node_parent->appendChild($node_child);
        if ($value !== null) { // add text node
            $node_childText = $this->domDocument->createTextNode($value);
            $node_child->appendChild($node_childText);
        }
        return $node_child;
    }
}

?>